package com.atguigu.gmall.controller;

import com.atguigu.gmall.cart.CartFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: 封捷
 * @create-date: 2022/9/6 14:57
 */
@Controller
public class CartWebController {

    @Autowired
    private CartFeignClient cartFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @RequestMapping("/addCart.html")
    public String addCart(
            // 接收前端请求参数
            @RequestParam("skuId") Long skuId,
            @RequestParam("skuNum") Integer skuNum,
            Model model) {

        // 执行添加购物车操作
        cartFeignClient.addToCart(skuId, skuNum);

        // 为了在“添加购物车成功”页面显示结果，查询 SkuInfo 信息
        Result<SkuInfo> skuInfoResult = productFeignClient.getSkuInfo(skuId);

        SkuInfo skuInfo = skuInfoResult.getData();

        // 存入模型
        model.addAttribute("skuInfo", skuInfo);
        model.addAttribute("skuNum", skuNum);

        return "cart/addCart";
    }

    // 前往购物车页面：只需要前往页面即可，数据是页面上另外发异步请求获取
    @RequestMapping("/cart.html")
    public String toShowCartPage() {
        return "cart/index";
    }

}
